package cn.smilefox.controller;

import cn.smilefox.domain.Role;
import cn.smilefox.domain.Teacher;
import cn.smilefox.service.RoleService;
import cn.smilefox.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/teacher")
@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private RoleService roleService;

    // 教师列表展示
    @RequestMapping("/list")
    public ModelAndView list(){

        ModelAndView modelAndView = new ModelAndView();
        List<Teacher> teacherList = teacherService.list();
        //设置模型
        modelAndView.addObject("teacherList",teacherList);
        //设置视图

        modelAndView.setViewName("teacher-list");

        return modelAndView;
    }

    //教师列表展示以及新建功能页面
    @RequestMapping("/lists")
    public ModelAndView lists(){
        ModelAndView modelAndView = new ModelAndView();
        List<Teacher> teacherList = teacherService.list();
        //设置模型
        modelAndView.addObject("teacherList",teacherList);
        //设置视图
        modelAndView.setViewName("teacherman-list");

        return modelAndView;
    }

    //新建功能跳转
    @RequestMapping("/saveUI")
    public ModelAndView saveUI(){

        ModelAndView modelAndView = new ModelAndView();

        List<Role> roleList = roleService.list();

        //设置模型
        modelAndView.addObject("roleList",roleList);
        //设置视图

        modelAndView.setViewName("teacher-add");

        return modelAndView;
    }
    //创建新teacher
    @RequestMapping("/save")
    public String save(Teacher teacher,Long[] roleIds){

        teacherService.save(teacher, roleIds);

        return "redirect:/teacher/list";
    }

    //删除用户
    @RequestMapping("/del/{teacherId}")
    public String del(@PathVariable("teacherId") Long teacherId){
        teacherService.del(teacherId);

        return "redirect:/teacher/list";

    }

    //查询teacher
    @RequestMapping("/find")
    public ModelAndView find(Teacher teacher){
        ModelAndView modelAndView = new ModelAndView();

        List<Teacher> teacherList = teacherService.find(teacher);
        //设置模型
        modelAndView.addObject("teacherList", teacherList);
        //设置视图

        modelAndView.setViewName("findteacher-list");

        return modelAndView;
    }

    // //-------------------课程工作量核算统计-----------------
    @RequestMapping("/coursenum")
    public ModelAndView modelAndViewnum(){
        ModelAndView modelAndView = new ModelAndView();
           List<Teacher> teacherList = teacherService.findallnum();
           modelAndView.addObject("teacherList",teacherList);
           modelAndView.setViewName("coursesnum-list");
           return modelAndView;
    };

    //--------------------课时搜索---------------------
    @RequestMapping("/findcoursenum")
    public ModelAndView modelAndViewnum(Teacher teacher){
        ModelAndView modelAndView = new ModelAndView();
        Teacher teacher1= teacherService.findCourseNum(teacher);
        //创建个数列存入teacher1
        List<Teacher> teacherList = new ArrayList<Teacher>();
        teacherList.add(teacher1);
        System.out.println(teacherList);
        modelAndView.addObject("teacherList",teacherList);
        modelAndView.setViewName("coursesnum-list");
        return modelAndView;
    };


}
